package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolPoseVo;
import com.hitqz.robot.api.common.entity.PatrolPose;
import com.hitqz.robot.api.common.entity.PatrolPoseGroup;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.service.PatrolPoseService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.QueryCameraParamDTO;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/patrolPose")
@Tag(name = "巡检位姿管理")
public class PatrolPoseController {

    private final PatrolPoseService patrolPoseService;
    private final RobotEquipmentService robotEquipmentService;
    private final EquipmentFactory equipmentFactory;

    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询巡检姿态")
    public R<PatrolPoseVo> getById(@PathVariable("id") Integer id) {
        PatrolPose patrolPose = patrolPoseService.getById(id);
        PatrolPoseVo vo = BeanUtil.copyProperties(patrolPose, PatrolPoseVo.class);
        return R.ok(vo);
    }

//    @PostMapping("/mark" )
//    @Operation(summary = "标定外设参数")
//    public R mark(@RequestParam(value = "equipmentId",required = false) Integer equipmentId) {
//        if (equipmentId == null) {
//            //标定一次所有需要标定的外设的参数，云台/顶升都是覆盖，机械臂是新增
//
//        }
//        return R.ok();
//    }


    @PutMapping
    @Operation(summary = "修改巡检位姿")
    public R updateById(@RequestBody @Validated PatrolPoseVo vo) {
        PatrolPose patrolPose = BeanUtil.copyProperties(vo, PatrolPose.class);
        return R.ok(patrolPoseService.updateById(patrolPose));
    }

    @DeleteMapping
    @Operation(summary = "根据id删除巡检位姿")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(patrolPoseService.removeBatchByIds(CollUtil.toList(ids)));
    }

    @GetMapping("/{id}/execute")
    @Operation(summary = "调用巡检位姿")
    public R execute(@PathVariable("id") Integer id,
                     @Parameter(description = "命令:0-回正 1-调用") @RequestParam(value = "cmd")Integer cmd) {
        try {
            PatrolPose patrolPose = patrolPoseService.getById(id);
            return patrolPoseService.executeAsync(patrolPose,cmd);
        }catch (Exception e){
            log.error("调用姿态失败",e);
            return R.failed("调用姿态失败");
        }
    }


    @GetMapping("/{id}/updatePose" )
    @Operation(summary = "更新单个姿态")
    public R updatePose(@PathVariable("id") Integer id) {
        PatrolPose patrolPose = patrolPoseService.getById(id);
        if (patrolPose==null){
            return R.failed("未找到姿态信息");
        }
        try {
            RobotEquipment equipment = robotEquipmentService.getById(patrolPose.getEquipmentId());
            IEquipmentService service = equipmentFactory.getServiceByBrand(equipment.getBrand());
            QueryCameraParamDTO queryDto = new QueryCameraParamDTO();
            queryDto.setIp(equipment.getIp());
            queryDto.setChannel(equipment.getChannel());
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setUsername(equipment.getUsername());
            queryParamsDto.setPort(equipment.getPort());
            queryParamsDto.setPassword(equipment.getPassword());
            queryParamsDto.setIp(equipment.getIp());
            queryParamsDto.setChannel(equipment.getChannel());
            List<EquipmentParamsDto> equipmentParamsDtos = service.getEquipmentParams(queryParamsDto);
            if (CollUtil.isNotEmpty(equipmentParamsDtos)) {
                patrolPose.setJson(JSON.toJSONString(equipmentParamsDtos));
                patrolPoseService.updateById(patrolPose);
            }
        }catch (Exception e){
            log.error("获取参数失败", e);
            return R.failed("获取外设姿态失败,请检查外设是否正常");
        }
        return R.ok();
    }


}
